Python os.sendfile()用法及代码示例 您所在的位置:网站首页 sent os Python os.sendfile()用法及代码示例

Python os.sendfile()用法及代码示例

2023-12-15 21:49| 来源: 网络整理| 查看: 265

Python中的OS模块提供了与操作系统进行交互的函数。操作系统属于Python的标准实用程序模块。该模块提供了使用依赖于操作系统的函数的便携式方法。

os.sendfile()Python中的方法用于从指定的偏移量开始,将指定数量的字节从指定的源文件描述符复制到指定的dest文件描述符。此方法返回发送的字节总数,如果达到EOF(文件末尾),则返回0。

用法: os.sendfile(dest, source, offset, count)

参数: dest:代表目标文件的文件描述符。 source:代表源文件的文件描述符 offset:代表起始位置的整数值。从该位置开始计算要发送的字节数。 count:一个整数值,表示要从源文件描述符发送的字节总数。

返回类型:此方法返回一个整数值,该整数值表示从源文件描述符发送到目标文件描述符的字节总数。如果达到EOF,则返回0。

将以下文本视为名为“ Python_intro.txt”的文件的内容。

Python is a widely-used general-purpose, high-level programming language. It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It was mainly developed for emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code. Python is a programming language that lets you work quickly and integrate systems more efficiently.

代码:用于os.sendfile()方法 # Python program to explain os.sendfile() method     # importing os module  import os       # Source file path source = './Python_intro.txt'    # destination file path dest = './newfile.txt'    # Open both files and get # the file descriptor # using os.open() method src_fd = os.open(source, os.O_RDONLY) dest_fd = os.open(dest, os.O_RDWR | os.O_CREAT)    # Now send n bytes from # source file descriptor # to destination file descriptor # using os.sendfile() method offset = 0 count = 100 bytesSent = os.sendfile(dest_fd, src_fd, offset, count) print("% d bytes sent / copied successfully." % bytesSent)    # Now read the sent / copied # content from destination # file descriptor  os.lseek(dest_fd, 0, 0) str = os.read(dest_fd, bytesSent)    # Print read bytes print(str)    # Close the file descriptors os.close(src_fd) os.close(dest_fd) 输出: 100 bytes sent/copied successfully. b'Python is a widely used general-purpose, high level programming language. It was initially designed '

参考: https://docs.python.org/3/library/os.html#os.sendfile



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有